package com.hl.secondHand.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hl.secondHand.common.lang.Result;
import com.hl.secondHand.common.vo.EasyForum;
import com.hl.secondHand.entity.Forum;
import com.hl.secondHand.mapper.ForumMapper;
import com.hl.secondHand.service.IForumService;
import com.hl.secondHand.service.IUserService;
import com.hl.secondHand.shiro.AccountProFile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hl
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/forum")
public class ForumController {
    @Autowired
    IForumService forumService;
    @Autowired
    ForumMapper forumMapper;
    @Autowired
    IUserService userService;
    @Autowired
    MailboxController mailboxController;

    @RequiresAuthentication
    @RequestMapping("/addForum")
    public Result addForum(Forum forum,String rForum){
        int nowId = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        if(forum.getRid()!=null){
            String replyname =  userService.getById(forum.getRid()).getName();
            forum.setContent("回复"+replyname+":"+forum.getContent());
            mailboxController.addMail(forum.getRid(),3,"您的评论'"+rForum+"'被回复了");
        }
        forum.setUid(nowId);
        forumService.saveOrUpdate(forum);
        return Result.succes(null);
    }

    @RequestMapping("/getEasyForum")
    public Result getEasyForum(int gid){
        List<EasyForum> forum = forumMapper.getEasyForum(gid);
        return Result.succes(forum);
    }

    @RequiresAuthentication
    @RequestMapping("/getForum")
    public Result getForum(){
        int nowId = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        List<Forum> forum = forumService.list(new QueryWrapper<Forum>().eq("uid",nowId).orderByDesc("created_date"));
        return Result.succes(forum);
    }

}
